package com.combetter.changxing.viewmodel;

import android.app.Application;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.ArrayMap;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.combetter.changxing.MyApp;
import com.combetter.changxing.model.EventMessage;
import com.combetter.changxing.model.History;
import com.combetter.changxing.net.service.APIService;
import com.combetter.changxing.util.Check;
import com.combetter.changxing.util.DateUtils;
import com.combetter.changxing.util.LogUtil;
import com.combetter.changxing.util.SharePreferencesUtils;
import com.combetter.changxing.util.ToastUtils;
import com.combetter.changxing.viewmodel.base.BaseViewModel;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by 深圳唐龙飞讯科技有限公司 on 2018/6/7.
 */

public class HistoryViewModel extends BaseViewModel {
    //历史bean
    public History history;

    public List<History.SmokeCountBean> listCount;
    public SharePreferencesUtils helpUtils;
    public Gson gson;
    //整理后的数据
    public Map<Integer, Map<String, List<History.SmokeCountBean>>> smokeCountMap = new ArrayMap<>();
    public Map<String, List<History.SmokeCountBean>> stringListMap = new HashMap<>();

    public HistoryViewModel(@NonNull Application application) {
        super(application);
        helpUtils = new SharePreferencesUtils(mContext);
        gson = new Gson();
    }


    /**
     * 查询历史吸烟记录
     */
    public void selectHistory(SmartRefreshLayout refreshLayout) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("uid", MyApp.getInstance().getUserInfo().getUid());
        map.put("date", DateUtils.getCurTime(DateUtils.FORMAT_SHORT));
        LogUtil.e("查询历史吸烟记录参数：", map);
        mHttpRequest.createService(APIService.class)
                .selectHistory(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).doOnError(throwable -> {
            refreshLayout.finishRefresh();
            EventBus.getDefault().post(new EventMessage("selectHistory", ""));
        })
                .subscribe(httpResult -> {
                    refreshLayout.finishRefresh();
                    LogUtil.e("查询历史吸烟记录返回：", httpResult);
                    if (httpResult.getCode() == 200) {
                        history = httpResult.getData();
                        listCount = history.getListCount();

                        if (listCount != null && listCount.size() > 0) {
                            helpUtils.clearMainHistory();
                            String mainData = gson.toJson(listCount);
                            helpUtils.saveMainHistory(mainData);
                        }
                        removeDuplicate(history.getSmokeCount());
                        EventBus.getDefault().post(new EventMessage("selectHistory", ""));
                    } else {
                        ToastUtils.error(httpResult.getMsg());
                    }
                });
    }

    /**
     * 处理后台返回的数据
     *
     * @param countBeans
     */
    private void removeDuplicate(List<List<History.SmokeCountBean>> countBeans) {
        if (Check.isEmpty(countBeans)) {
            return;
        }
        for (int i = 0; i < countBeans.size(); i++) {
            String goodsName = "";
            for (int j = 0; j < countBeans.get(i).size(); j++) {
                if (!TextUtils.isEmpty(countBeans.get(i).get(j).getGoodsName())) {
                    goodsName = countBeans.get(i).get(j).getGoodsName();
                }
            }
            stringListMap.put(goodsName, countBeans.get(i));
            smokeCountMap.put(i, stringListMap);
        }
        if (stringListMap != null && stringListMap.size() > 0) {
            helpUtils.clearOtherHistory();
            String data = gson.toJson(stringListMap);
            helpUtils.savOtherHistory(data);
        }

//        List<Integer> integers = new ArrayList<>();
//        for (List<History.SmokeCountBean> bean : countBeans) {
//            integers.add(bean.getSmid());
//        }
        //集合去重
//        List<Integer> listNew = new ArrayList<>(new TreeSet<Integer>(integers));
        //临时集合
//        List<List<History.SmokeCountBean>> temList = null;
////        ArrayList<String> dateList = DateUtils.getArbitrarilyDate(7, "MM-dd");
//        //循环烟类型获取集合分类数据
//        for (Integer integer : listNew) {
//            temList = new ArrayList<>();
//            for (List<History.SmokeCountBean> bean : countBeans) {
//                if (bean.getSmid() == integer) {
//                    temList.add(bean);
//                }
//            }
//            smokeCountMap.put(integer, temList);
//        }
    }
}
